//
//  CCMUtilExtension.swift
//  CCMoney_swift
//
//  Created by JKHuang on 2018/11/23.
//  Copyright © 2018 JKHuang. All rights reserved.
//

import UIKit


extension UIColor {
    
    static func rgb(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat) -> UIColor {
        return UIColor.init(red: r / 255,
                            green: g / 255,
                            blue: b / 255,
                            alpha: 1.0)
    }
    
    static func hex(_ Hex: UInt32) -> UIColor {
        return UIColor.init(red: CGFloat((Hex & 0xFF0000) >> 16) / 255.0,
                            green: CGFloat((Hex & 0xFF00) >> 8) / 255.0,
                            blue: CGFloat((Hex & 0xFF)) / 255.0,
                            alpha: 1.0)
    }
    
    /// 随机颜色
    static func random() -> UIColor {
        return UIColor(red: CGFloat(arc4random_uniform(256)) / 255.0,
                       green: CGFloat(arc4random_uniform(256)) / 255.0,
                       blue: CGFloat(arc4random_uniform(256)) / 255.0, alpha: 1.0)
    }
}

extension String {
    
    /// 判断电话号码是否正确
    func isPhoneNumber() -> Bool {
        let pattern = "^(0|86)?1(([358][0-9])|(7[678])|(4[57]))\\d{8}$"
        guard let expression = try? NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0)) else {
            return false
        }
        
        let result = expression.firstMatch(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSRange(location: 0, length: self.count))
        if result == nil {
            return false
        }
        return true
    }
    
    /// 是否纯数字
    func isNumber() -> Bool {
        var res = true
        let numberSet = CharacterSet(charactersIn: "0123456789")
        var i = 0
        let str = self as NSString
        while i < str.length {
            let subString = str.substring(with: NSMakeRange(i, 1))
            let range = subString.rangeOfCharacter(from: numberSet)
            if range == nil || range!.isEmpty {
                res = false
                break
            }
            i += 1
        }
        return res
    }
}

